
function getQueryString() {
  let name, value;
  let str = location.href; //取得整个地址栏
  str = str.replace(/\?from.*?#\//g,'#/');
  let num = str.indexOf("?")
  str = str.substr(num + 1); //取得所有参数
  let arr = str.split("&"); //各个参数放到数组里
  for (let i = 0; i < arr.length; i++) {
    num = arr[i].indexOf("=");
    if (num > 0) {
      name = arr[i].substring(0, num).toLowerCase();
      value = arr[i].substr(num + 1);
      this[name] = value;
    }
  }
}

/**
 * 存储localStorage
 */
const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
const getStore = name => {
  if (!name) return;
  const dataStore = window.localStorage.getItem(name);
  try {  
    if (typeof JSON.parse(dataStore) == "object") {  
      return JSON.parse(dataStore);
    }  
  }catch(e) {  
    return dataStore;
  }  
  return dataStore;
}

/**
 * 删除localStorage
 */
const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
}

//sessionStorage
const setSession = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  sessionStorage.setItem(name, content);
}

/**
 * sessionStorage
 */
const removeSession = name => {
  if (!name) return;
  window.sessionStorage.removeItem(name);
}

const getSession = name => {
  if (!name) return;
  const dataStore = sessionStorage.getItem(name);
  try {  
    if (typeof JSON.parse(dataStore) == "object") {  
      return JSON.parse(dataStore);
    }  
  }catch(e) {  
    return dataStore;
  }  
  return dataStore;
}

const transTime = (S,AM) => {
  let T = new Date(1E3 * S),
      Format = Q => {return Q < 10 ? '0' + Q : Q},
      result = Format(T.getHours()) + ':' + Format(T.getMinutes());
  let oneDay = 60 * 60 * 24;//s
  let zeroTime = new Date().setHours(0,0,0,0)/1000;
  let passTwo = zeroTime - oneDay;//昨天凌晨
  if(S > passTwo && S < zeroTime){
    result ='昨天 ' + result;
  }else if(S < zeroTime){
    let month = T.getMonth() + 1,
        days = T.getDate();
        result = month + '月' + days + '日 ' + result;
  }else{
    if(typeof AM !== 'undefined'){
      let tHour = T.getHours(),
          tDetail = tHour > 12 ? '下午 ':'上午 ';
      result = tDetail + result;
    }
  }
  return result;
}

const formatData = Q =>{
  return Q < 10 ? '0' + Q : Q;
};

const isIOS = () =>{ 
  let u = navigator.userAgent, app = navigator.appVersion;   
  //let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器   
  return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端   
}

const Client = {
  getQueryString,
  setStore,
  getStore,
  removeStore,
  transTime,
  isIOS,
  formatData,
  setSession,
  getSession,
  removeSession
}
export default Client